package com.suypower.carManage.json;

import com.jarp.db.DAO;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.suypower.carManage.VO.TreeVO;
import com.suypower.carManage.comm.ConstUtil;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

/**
 * Created by zhaoyuan on 14-2-18.
 */
public class RoleListJson extends ActionSupport{

    public String execute(){
        String res="";
        DAO dao=new DAO();
        ArrayList<TreeVO> list=new ArrayList<TreeVO>();
        String sql="select * from tb_role";
        try {
            Vector v=dao.getDataSet(ConstUtil.DBNAME,sql);
            if(v!=null){
                for(int i=0;i<v.size();i++){
                    Hashtable hashtable= (Hashtable) v.get(i);
                    TreeVO treeVO=new TreeVO();
                    treeVO.setText((String)hashtable.get("NAME"));
                    treeVO.setId((String)hashtable.get("ID"));
                    treeVO.setCode((String)hashtable.get("VALUE"));
                    treeVO.setLeaf(true);
                    list.add(treeVO);
                }
                JSONArray json = new JSONArray();
                json = JSONArray.fromObject(list);
                res = json.toString();
            }
        } catch (ServletException e) {
            e.printStackTrace();
        }
        ActionContext.getContext().put("data", res);
        return SUCCESS;
    }
}
